package projetbi_extraction;

// Java standard + POI
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

// Nos classes
import tools.DocxListCreator;
import static tools.DocxListCreator.getExtension;
import projetbi_parsing.StringPatternMatching;

/**
 * Projet BI - M2 FIC 2012-2013
 *
 * @author Jef, Tuan-Long, William, Mohamed
 */
public class BrowseThroughFiles {

    public BrowseThroughFiles() {
        // Vide.
    }
    static int spc_count = -1;

    // Méthode pour parcourir récursivement les fichiers
    // et appeler le traitement uniquement sur les fichiers .docx
    public static void BrowseThroughFiles(File aFile)
            throws FileNotFoundException, IOException, InterruptedException {
        spc_count++;
        String spcs = "";
        ProcessEachFile pef = new ProcessEachFile();

        for (int i = 0; i < spc_count; i++) {
            spcs += " ";
        }
        if (aFile.isFile()) {
            System.out.println(spcs + "[Fichier] " + aFile.getPath());
            String extension = getExtension(aFile.getPath());

            if (extension.equalsIgnoreCase("docx")) {
                // Traitement sur les fichiers .docx
                System.out.println(spcs + "[Extension] " + extension);
                FileInputStream fis = new FileInputStream(aFile.getAbsolutePath());
                XWPFDocument doc = new XWPFDocument(fis);
                XWPFWordExtractor docExtractor = new XWPFWordExtractor(doc);
                // Ici, le docExtractor.getText() doit être processé
                System.out.println(docExtractor.getText());
                System.out.println("___________________________________________");
                pef.processFullCV(docExtractor.getText());
                //System.out.println(docExtractor.getDocument().toString());
            }

        } else if (aFile.isDirectory()) {
            System.out.println(spcs + "[Dossier] " + aFile.getPath());
            File[] listOfFiles = aFile.listFiles();
            if (listOfFiles != null) {
                for (int i = 0; i < listOfFiles.length; i++) {
                    BrowseThroughFiles(listOfFiles[i]);
                }
            } else {
                System.out.println(spcs + " [Accès refusé] ");
            }
        }
        spc_count--;
    }
}
